import '../css/index.min.css';
const eleves = document.getElementById('eleves');
import './loading';
function running(csclass) {
    eleves.setAttribute('class', csclass)
}
const thiselevs = {
    runtop() {
        running("eleveshou");
    },
    runbottom() {
        running("eleveszheng");
    },
    runleft() {
        running('elevesleft');
    },
    runright() {
        running('elevesright');
    },
    runtopstop() {
        running('eleveshoustop');
    },
    runbottomstop() {
        running('eleveszhengstop');
    },
    runleftstop() {
        running('elevesleftstop');
    },
    runrightstop() {
        running('elevesrightstop');
    }
}
thiselevs.runleftstop();
function stop(dir) {
    switch (dir) {
        case "top":
            setTimeout(() => {
                thiselevs.runtopstop();
                keys = true;
            }, 2000)
            break;
        case "left":
            setTimeout(() => {
                thiselevs.runleftstop();
                keys = true;
            }, 2000)
            break;
        case "bottom":
            setTimeout(() => {
                thiselevs.runbottomstop();
                keys = true;
            }, 2000)
            break;
        case "right":
            setTimeout(() => {
                thiselevs.runrightstop();
                keys = true;
            }, 2000)
            break;
    }
}

function direction(left, top){
    let number = parseInt(Math.random() * 100);
    console.log(number);
    if (number >= 50) {
        console.log(parseInt(eleves.style.top) - top)
        if (parseInt(eleves.style.top) - top > 0) {
            thiselevs.runtop()
            eleves.style.top = top + 'px';
            eleves.style.left = left + 'px';
     
            stop("top");
        } else if (parseInt(eleves.style.top) - top < 0) {
            thiselevs.runbottom()
            eleves.style.top = top + 'px';
            eleves.style.left = left + 'px';
            stop("bottom");
 
        }
    } else if (number < 50) {
        console.log(parseInt(eleves.style.top) - top)
        if (parseInt(eleves.style.left) - left > 0) {
            thiselevs.runleft()
            eleves.style.top = top + 'px';
            eleves.style.left = left + 'px';
            stop("left");
            demo.one(top,left)
        } else if (parseInt(eleves.style.left) - left < 0) {
            thiselevs.runright()
            eleves.style.top = top + 'px';
            eleves.style.left = left + 'px';
            stop("right");
            demo.one(top,left)
        }
    }
}
function random(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}
var keys = true;
function rungs() {
    window.timer = setInterval(() => {
        if(keys){
            setTimeout(() => { 
                let x = random(50, 1050);
                let y = random(230, 430);
                direction(x, y);
            }, random(2000, 20000)); 
            keys = false;
        }
  
    }, 1000);
}
eleves.style.top = 400 + 'px';
eleves.style.left = 150 + 'px';

const btneat = document.getElementsByClassName('eat')[0],
      btnsleap = document.getElementsByClassName('sleap')[0],
      btnfumo = document.getElementsByClassName('fumo')[0],
      hunger = document.getElementsByClassName('hunger')[0],
      health = document.getElementsByClassName('health')[0],
      agitated = document.getElementsByClassName('agitated')[0],
      tages1 = document.getElementsByClassName('tages1')[0],
      tages2 = document.getElementsByClassName('tages2')[0],
      tages3 = document.getElementsByClassName('tages3')[0],
      ul = document.getElementsByTagName('ul'),
      i = document.getElementsByTagName('i');
      tages1.style.marginLeft = -300 + 'px';
      tages2.style.marginLeft = -300 + 'px';
      tages3.style.marginLeft = -300 + 'px';
function huishou(houth){
    setTimeout(()=>{
        houth.style.marginLeft = -300 + 'px'
    },2000)
}

const state = {
    hungers : 100,
    healths : 100,
    agitated : 100,
    goodhunger(){
        hunger.style.height = state.hungers - 10 + '%';
        if(this.hungers >= 100){
            
            let keys = true;
            tages1.innerText = '我已经很饱了';
            tages1.style.marginLeft = 0;
            if(keys){
                keys = false;
                huishou(tages1);
            }
        }else{
            this.hungers+= random(1,5);
        }
    },
    goodhealth(){
        health.style.height = state.hungers - 10 + '%';
        if(this.healths >= 100){
            let keys = true;
            tages2.innerText = '我感觉身体足够健康';
            tages2.style.marginLeft = 0
            if(keys){
                keys = false;
                huishou(tages2);
            }
        }else{
            this.healths+= random(1,5);
        }
    },
    goodagitated(){
        agitated.style.height = state.hungers - 10 + '%';
        if(this.agitated >= 100){
            let keys = true;
            tages3.innerText = '我感觉很开心';
            tages3.style.marginLeft = 0
            if(keys){
                keys = false;
                huishou(tages3);
            }
        }else{
            this.agitated+= random(1,5);
        }
    },
    nothunger(){
        hunger.style.height = state.hungers - 10 + '%';
        if(this.hungers <= 10){
            
            let keys = true;
            tages1.innerText = '我的感觉要饿死了';
            tages1.style.marginLeft = 0;
            if(keys){
                keys = false;
                huishou(tages1);
            }
        }else{
            this.hungers-= random(1,5);
        }
    },
    nothealth(){
        health.style.height = state.hungers - 10 + '%';
        if(this.healths <= 10){
            let keys = true;
            tages2.innerText = '我感觉身体马上就要垮掉';
            tages2.style.marginLeft = 0
            if(keys){
                keys = false;
                huishou(tages2);
            }
        }else{
            this.healths-= random(1,5);
        }
    },
    notagitated(){
        agitated.style.height = state.hungers - 10 + '%';
        if(this.agitated <= 10){
            let keys = true;
            tages3.innerText = '我已经悲伤到了极点';
            tages3.style.marginLeft = 0
            if(keys){
                keys = false;
                huishou(tages3);
            }
        }else{
            this.agitated-= random(1,5);
        }
    }
    }
    hunger.style.height = state.hungers - 10 + '%';
    health.style.height = state.healths - 10 + '%';
    agitated.style.height = state.agitated - 10 + '%';
    btneat.addEventListener('click',()=>{
        state.goodhunger();
    })
    btnsleap.addEventListener('click',()=>{
        state.goodhealth();
    })
    btnfumo.addEventListener('click',()=>{
        state.goodagitated();
    })

    function fors(){
        setTimeout(()=>{
            setInterval(() => {
                if(state.hungers >= 10){
                    state.nothunger();
                    console.log('饱食度减少了')
                }
            }, 5000);
            setInterval(() => {
                if(state.healths >= 10){
                    state.nothealth();
                    console.log('健康减少了')
                }
            }, 15000);
            setInterval(() => {
                if(state.agitated >= 10){
                    state.notagitated();
                    console.log('心情减少了')
                }
            }, 10000);
        },20000)
    }
function none(num){
    const noe = [eleves,btneat,btnsleap,btnfumo,hunger,health,agitated,tages1,tages2,tages3,...ul,...i]
    switch(num){
        case 'none':
            for(let i of noe.values()){
                i.style.display = 'none';
            }
            break;
        case 'true':
            for(let i of noe.values()){
                i.style.display = 'block';
            }
            break;
    }
}

none('none');
import imgs from '../img/backgrougnthis.png';
import evels from '../img/eleves.png';
let evelss = evels;
function loading(){   
    const img = new Image();
        img.src = imgs;
        img.onload = function(){
            let bgis = document.getElementsByClassName('gamebgc')[0];
            bgis.style.backgroundSize= "100% 100%";         
            bgis.style.backgroundRepeat = "no-repeat"; 
            bgis.style.backgroundColor = "none";
            bgis.style.backgroundImage = 'none';
            bgis.appendChild(img);
            img.style.width = '100%'
            img.style.height = '100%'
            console.log('总加载完成')
            none('true');
        }
    
}
loading()
fors()
rungs()


